package com.wlt.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wlt.pojo.Specification;
import com.wlt.pojo.SpecificationOptions;
import com.wlt.result.BaseResult;
import com.wlt.service.SpecificationService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品规格
 */
@RestController
@RequestMapping("/specification")
public class SpecificationController
{
    @DubboReference
    private SpecificationService specificationService;
    
    /**
     * 新增
     * @param specification 商品规格对象
     * @return              执行结果
     */
    @PostMapping("/add")
    public BaseResult add (@RequestBody Specification specification)
    {
        specificationService.add(specification);
        
        return BaseResult.success();
    }
    
    /**
     * 修改
     * @param specification 商品规格对象
     * @return              返回结果
     */
    @PutMapping ("/update")
    public BaseResult update (@RequestBody Specification specification)
    {
        specificationService.update(specification);
        
        return BaseResult.success();
    }
    
    /**
     * 删除
     * @param ids   id数组
     * @return      执行结果
     */
    @DeleteMapping ("/delete")
    public BaseResult delete (Long[] ids)
    {
        specificationService.delete(ids);
        
        return BaseResult.success();
    }
    
    /**
     * 根据id查询
     * @param id    商品规格id
     * @return      执行结果
     */
    @GetMapping ("findById")
    public BaseResult<Specification> findById (Long id)
    {
        Specification byId = specificationService.findById(id);
        
        return BaseResult.success(byId);
    }
    
    /**
     * 分页查询
     * @param page  页码
     * @param size  每页条数
     * @return      执行结果
     */
    @GetMapping("/search")
    public BaseResult<Page<Specification>> search (int page, int size)
    {
        Page<Specification> search = specificationService.search(page, size);
        
        return BaseResult.success(search);
    }
    
    /**
     * 根据商品类型查询
     * @param id    商品类型id
     * @return      执行结果
     */
    @GetMapping ("/findByProductTypeId")
    public BaseResult<List<Specification>> findByProductTypeId (Long id)
    {
        List<Specification> byProductTypeId = specificationService.findByProductTypeId(id);
        
        return BaseResult.success(byProductTypeId);
    }
    
    /**
     * 新增商品规格项
     * @param specificationOptions  商品规格项对象
     * @return                      执行结果
     */
    @PostMapping("/addOption")
    public BaseResult addOption (@RequestBody SpecificationOptions specificationOptions)
    {
        specificationService.addOption(specificationOptions);
        
        return BaseResult.success();
    }
    
    /**
     * 删除商品规格项
     * @param ids   商品规格项的id集合
     * @return      执行结果
     */
    @DeleteMapping ("/deleteOption")
    public BaseResult deleteOption (Long[] ids)
    {
        specificationService.deleteOption(ids);
        
        return BaseResult.success();
    }
}
